﻿$(document).ready(function () {
    //invoke the Ajax Login method
    $("#frmLogin").submit(function (e) {
        var loginValues = $("#frmLogin").serialize();
        $.validator.unobtrusive.parse($("#frmLogin"));

        if ($("#frmLogin").valid()) {
            $.post(pathPrefix + "Admin/Login", loginValues, function (result) {
                var mainDialog = updateDialog("התחברות משתמש", "100", "400");
                mainDialog.html(result.Value);

                //if the login result is true
                if (result.Key) {
                    window.setTimeout(function () {
                        window.location = pathPrefix + "Admin";
                    }, 1000);
                }
            }, "json");
        }

        e.preventDefault();
    });

    $("#frmEditorLogin").submit(function (e) {
        $("#loginFailed").hide();

        var loginValues = $(this).serialize();
        $.validator.unobtrusive.parse($(this));

        if ($(this).valid()) {
            $.post(pathPrefix + "Users/EditorLogin", loginValues, function (result) {
                //if the login result is true
                if (result.Key) {
                    //move to specified page
                    window.location = result.Value;
                }
                else {
                    $("#loginFailed").show();
                }
            }, "json");
        }

        e.preventDefault();
    });

    $("#Password").keydown(function () {
        $("#loginFailed").hide();
    });

    $("#AlbumId").change(function () {
        var albumId = $(this).val();

        $.post(pathPrefix + "Albums/SectionsOfAlbum", { "id": albumId }, function (result) {
            $("#AlbumSections option:not(:first)").remove();

            //insert all options to the sections DDL
            for (var i = 0; i < result.length; i++) {
                var option = $("<option>");
                option.attr("value", result[i].AlbumSectionId);
                option.html(result[i].SectionTitle);
                $("#AlbumSections").append(option);
            }
        }, "json");
    });

    $("#AlbumId").change();

    $("#AlbumSections").change(function () {
        //save the album section id
        var sectionId = $(this).val();
        $("#AlbumSectionId").val(sectionId);
    });
});